package mbsl.commons.lib;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * The element class is the basis for all the top-level elements, like Game and Movie.
 */
public abstract class Element<T extends Element<T>> extends BasicElement {
	public Element() {
		super();
		
		mRating = RATING_NOT_SET; 
	}
	/**
	 * Returns -1 if no rating is set.
	 */
	public int getRating() {
		return mRating;
	}
	/**
	 * Use {@link #RATING_NOT_SET} to specify no rating.
	 * @param pRating
	 */
	public void setRating(int pRating) {
		mRating = pRating;
	}
	public Genre getGenre() {
		return mGenre;
	}

	public void setGenre(Genre pGenre) {
		mGenre = pGenre;
	}
	public Location getLocation() {
		return mLocation;
	}
	public void setLocation(Location pLocation) {
		mLocation = pLocation;
	}
	public List<Comment<T>> getComments() {
		return mComments;
	}
	public void setComments(List<Comment<T>> pComments) {
		mComments = pComments;
	}
	public String getYear() {
		return mYear;
	}
	public void setYear(String pYear) {
		mYear = pYear;
	}
	public T getParent() {
		return mParent;
	}
	@SuppressWarnings("unchecked")
	public void setParent(T pGame, boolean pUpdateParent) {
		if (pUpdateParent) {
			if (pGame != getParent() && getParent() != null) {
				if (getParent().getChildren().contains(this))
					getParent().getChildren().remove(this);
			} else {
				if (pGame != null && !pGame.getChildren().contains(this))
					pGame.getChildren().add((T)this);
			}
		}

		setParent(pGame);
	}
	public void setParent(T pElement) {
		mParent = pElement;
	}
	public Set<T> getChildren() {
		if (mChildren != null)
			return mChildren;
		else
			return mChildren = new HashSet<T>();
	}
	public void setChildren(Set<T> pChildren) {
		mChildren = pChildren;
	}
	
	private T mParent;
	private Set<T> mChildren;
	private Location mLocation;
	private Genre mGenre;
	private int mRating;
	private List<Comment<T>> mComments;
	private String mYear;

	/**
	 * The value indicating unset.
	 */
	public static final int RATING_NOT_SET = -1; // A change here needs to be done in the database as well.
}
